<?php
namespace backend\controllers;
use backend\models\Image;
use Yii;
use yii\filters\PageCache;
use yii\web\Controller;
use yii\web\UploadedFile;
use yii\data\Pagination;
class ImageController extends Controller{

    public function actionAdd(){
        $model = new Image();
        if($model->load(Yii::$app->request->post()) && $model->validate()){
            $data = Yii::$app->request->post();
            $model->title = $data['Image']['title'];
            $model->create_time = time();
            if($model->save()){
                Yii::$app->session->setFlash('info', '图片添加成功');
            }
        }
        return $this->render('img_add',[
            'model'=>$model,
        ]);
    }

    public function actionList(){
        $model = Image::find();
        $count = $model->count();
        $pageSize = Yii::$app->params['pageSize']['image'];
        $pager = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
        $images = $model->offset($pager->offset)->limit($pager->limit)->asArray()->all();
        return $this->render('img_list', ['images' => $images, 'pager' => $pager]);
    }

    public function actionDel(){
        if(Yii::$app->request->isPost){
            $img_id = Yii::$app->request->get('id');
            Image::deleteAll('id = :id', [':id' => $img_id]);
        }
        return $this->redirect(['list']);
    }

}